package cn.flying.cloud.base.feign;

import javax.annotation.Resource;

import java.util.concurrent.TimeUnit;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import okhttp3.ConnectionPool;

import cn.flying.cloud.base.properties.FeignProperties;

/**
 * 配置feign客户端调用采用OKHttp方式
 *
 * @author: admin
 * @date: 2022年04月12日 23:36
 * @version: 1.0
 */
@Configuration
public class FeignOkHttpConfig {

    @Resource
    private FeignProperties feignProperties;

    /**
     * Spring IOC容器初始化时构建okHttpClient对象
     *
     * @return
     */
    @Bean
    public okhttp3.OkHttpClient okHttpClient() {
        return new okhttp3.OkHttpClient.Builder()
                //读取超时时间
                .readTimeout(feignProperties.getReadTimeout(), TimeUnit.SECONDS)
                //连接超时时间
                .connectTimeout(feignProperties.getConnectTimeout(), TimeUnit.SECONDS)
                //写超时时间
                .writeTimeout(feignProperties.getWriteTimeout(), TimeUnit.SECONDS)
                //设置连接池
                .connectionPool(new ConnectionPool(feignProperties.getMaxIdleConnections(), feignProperties.getKeepAliveDuration(), TimeUnit.MINUTES))
                .build();
    }

}
